<template>
    <div>

        <div class="return-bar">
            <i class="iconfont" @click="back()">&#xe668</i>
            注册成为借款人
        </div>

        <div  class="top review"
        style="margin-top: 1.5rem; "
        v-if=" applied ">
            <group>
                <cell title="报单渠道:"
                :value.sync="viewData.company" ></cell>
            </group>

            <group>
                <cell title="借款人类型:"   >
                    <div>
                        个人借款
                        <span style="color: green;">(申请通过)</span>
                    </div>
                </cell>
            </group>

            <group>
                <cell title="授权委托书:"   >


                    <div v-if=" viewData.permit " >
                        已申请自动签章授权
                    </div>

                    <div v-else>
                        未申请自动签章授权
                        <span  class="gr_btn"  style="padding: 5px 10px;"
                        @click="signContract"
                        >
                            <i class="iconfont" style="font-size: 14px;">&#xe62f;</i>申请
                        </span>
                    </div>

                </cell>
            </group>


            <div class="fb_box1">

                <div class="ux_row">
                    <div class="ux_flex">
                        <button class="gr_btn"  @click="back()">返回</button>
                    </div>
                </div>
            </div>
        </div>



        <div  v-if=" !applied "
        class="borrower top"
        style="margin-top: 1.5rem;padding: 0px 15px;">
            <group title="请在下接列表中选择您报单的公司">
                <selector placeholder="请选择您报单公司"
                :value.sync="postData.c_id"
                 :options="company_list"
                 ></selector>
            </group>

            <group title="借款人类型:">
                <cell title="个人借款"></cell>
            </group>




                    <group title="姓名：">
                        <x-input type="text"
                        readonly
                        :value.sync="postData.p_name"
                        placeholder="请输入姓名" ></x-input>
                    </group>

                    <group title="身份证号：">
                        <x-input type="text"
                        readonly
                        :value.sync="postData.p_idcard"
                        placeholder="请输入身份证号" ></x-input>
                    </group>



            <!-- <div class="ux_row">
                <div class="ux_flex">
                    <button class="gr_btn">申请电子签名</button>
                </div>

                <div class="ux_flex">
                    <button class="gr_btn_nobg"  @click="back()">取消</button>
                </div>


            </div> -->


            <div class="fb_box1">

                <div class="ux_row">
                    <div class="ux_flex">
                        <button class="gr_btn" @click=" Apply() ">申请电子签名</button>
                    </div>

                    <div class="ux_flex">
                        <button class="gr_btn_nobg"  @click="back()">取消</button>
                    </div>
                </div>
            </div>

        </div>

        <com-alert
        :show.sync="alertData.show"
        :title.sync="alertData.title"
        :buttontxt.sync="alertData.buttontxt"
        :content.sync="alertData.content"
        ></com-alert>

    </div>
</template>

<script>
import {Group, Selector, Radio, XInput, Cell} from 'vux';
import api from '../../../data/api';
import ComAlert from '../../../components/common/alert'
import { mapGetters } from 'vuex'
import LRZ from 'lrz'
var vm = this;
export default {


    data(){
        return{
            applied:false,
            company_list: [],

            alertData:{show:false,title:'标题',buttontxt:'确定',  content:'sign' },

            cType:'2',


            value:'帅哥',

            viewData:{
                company:'',
                type:'',
                edit:'',
                permit:false
            },

            postData:{
                type:'1',
                c_id:'',
                p_name:'',
                p_idcard:''
            }
        }
    },

    components:{
        ComAlert,
        Cell,
        XInput,
        Group,
        Selector,
        Radio
    },

    computed: {
		...mapGetters([
            'faAllStatus', 'warrantorInfo'
        ])
    },


    methods: {

        back(){
            this.$route.router.go('/user')
        },



        chooseCorp(event){
            vm.cType = event
        },


        async signContract(){
            vm.$store.dispatch('setShowLoading', true);
            var res = await api.user.borrower_contract_permit();
            if(res.status)
                window.location.href = res.url;
            else
                vm.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:res.msg};
            vm.$store.dispatch('setShowLoading', false);
        },

        async editView(){
            vm.applied = false;
            vm.$store.dispatch('setShowLoading', true);
            var res = await api.user.get_fadd_channel();
            vm.$store.dispatch('setShowLoading', false);
            for(var i=0; i<res.data.length;i++){
                vm.company_list.push({
                    key: res.data[i].id,
                    value: res.data[i].name
                });
            }
        },

        async Apply(){


            //tender_open_status 	投资人申请签章状态
            //tender_auto_status	投资人自动签章授权状态

            if(vm.faAllStatus.tender_open_status == false || vm.faAllStatus.tender_auto_status == false){
                vm.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:'你还没有申请 投资人签章或自动签章授权 !'};
                setTimeout(function() {
                    vm.$route.router.go('/user/sign-contract');
                }, 2000);
                return false;
            }

            vm.$store.dispatch('setShowLoading', true);

            var res = await api.user.borrower_sign_contract(vm.postData);
            if(res.status) vm.init();
            vm.alertData = {show:true, title:'信息提示', buttontxt:'确定', content:res.msg};
            vm.$store.dispatch('setShowLoading', false);

        },

        async init(){
            var res;
            await vm.$store.dispatch('getFadAllStatus');
            vm.applied = vm.faAllStatus.borrow_open_status;

            //await vm.$store.dispatch('getWarrantorInfo');
            res = await api.user.fadada_basic_info();

            vm.postData.p_name = res.data.realname;
            vm.postData.p_idcard = res.data.bank_idno;

            if(vm.applied){

                res = await api.user.borrower_info();
                vm.viewData = {
                    company:res.data.c_name,
                    //type: vm.warrantorInfo.type == '2' ? '公司担保':'个人担保',
                    //edit: vm.warrantorInfo.verify_status == '1' ? false:true,
                    permit:vm.faAllStatus.borrow_auto_status
                };


            }else{
                vm.editView();
            }


        }
    },

    route: {
        activate ( transition ) {
            vm = this;
            transition.next();
            vm.init();
        }
    }
}
</script>

<style lang="scss">

.borrower{


    .weui_cells_title{
        padding-left:0px;
        font-size:16px;
    }

    /* .gr_btn, .gr_btn_nobg{
        width:80%;
        padding: 10px;
        font-size: 16px;
        margin: auto;
        margin-top:20px;
        display: block;
    }
    .gr_btn_nobg{
        padding:9px 10px;
    } */




}

.review{
    .weui_cell_ft{
        font-size:15px;
    }

    .p_btn_nob{
        width:50%;
        padding: 10px;
        font-size: 16px;
        margin: auto;
        margin-top:20px;
        display: block;
        padding:9px 10px;
    }
}
</style>
